﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace Benefit.Service.Sys
{
    /// <summary>
    /// 交易历史表(需要)
    /// </summary>
    public class ServiceTradeHistory : Benefit.Interface.Sys.ITradeHistory
    {
        Benefit.DB.DBManager db = null;
        public ServiceTradeHistory(Benefit.DB.DBManager db)
        {
            this.db = db;
        }
        /// <summary>
        /// 判断当前输入的日期是否已经被传输入过数据库ok
        /// </summary>
        /// <param name="pdate"></param>
        /// <returns></returns>
        public bool HasChangeIntoDb(string pdate)
        {
            bool hasChanged = false;
            int year = Convert.ToInt32(pdate.Substring(0, 4));
            int month = Convert.ToInt32(pdate.Substring(4, 2));
            int day = Convert.ToInt32(pdate.Substring(6, 2));
            var query = from d in db.TradeHistorys where d.DPdate == day && d.MPdate == month && d.YPdate == year select d;
            if (query.Count() > 0)
            {
                hasChanged = true;
            }
            return hasChanged;
        }
        /// <summary>
        /// 获取前一个交易日
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public Models.Sys.TradeHistory GetPreviousTradingDay(int tradeHistoryId)
        {
            var query = db.TradeHistorys.Where(a => a.Id < tradeHistoryId).OrderByDescending(a => a.Id);
            if (query.Count() > 0)
            {
                return query.First();
            }
            else
            {
                return this.GetModel(tradeHistoryId);
            }
        }
        /// <summary>
        /// 获取上周的最后一个交易日
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public Models.Sys.TradeHistory GetPreviousWeekLastTradingDay(int tradeHistoryId)
        {
            Models.Sys.TradeHistory th = this.GetModel(tradeHistoryId);
            var query = db.TradeHistorys.Where(a => a.YPdate <= th.YPdate).Where(a => a.WPdate < th.WPdate).OrderByDescending(a => a.YPdate).OrderByDescending(a => a.WPdate).OrderByDescending(a => a.Id);
            if (query.Count() > 0)//如果有更小的周
            {
                th = query.First();
            }

            return th;
        }


        /// <summary>
        /// 给没有日期横岗的日期加上横岗
        /// </summary>
        /// <param name="pdate"></param>
        /// <returns></returns>
        public string AddHorizontalLineIntoPdate(string pdate)
        {
            string s = "";
            int year = Convert.ToInt32(pdate.Substring(0, 4));
            int month = Convert.ToInt32(pdate.Substring(4, 2));
            int day = Convert.ToInt32(pdate.Substring(6, 2));
            s = year + "-" + (month > 9 ? "" + month : "0" + month) + "-" + (day > 9 ? "" + day : "0" + day);
            return s;
        }

        /// <summary>
        /// 查询月的最后一个交易日
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns></returns>
        public Models.Sys.TradeHistory GetMonthLastTradingDay(int year, int month)
        {
            Models.Sys.TradeHistory history = new Models.Sys.TradeHistory();
            var query = db.TradeHistorys.Where(a => a.YPdate == year).Where(a => a.MPdate == month).OrderByDescending(a => a.Id);
            if (query.Count() > 0)
            {
                history = query.First();
            }
            return history;
        }
        /// <summary>
        /// 查询年的最后一个交易日
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns></returns>
        public Models.Sys.TradeHistory GetYearLastTradingDay(int year)
        {
            Models.Sys.TradeHistory history = new Models.Sys.TradeHistory();
            var query = db.TradeHistorys.Where(a => a.YPdate == year).OrderByDescending(a => a.YPdate).OrderByDescending(a => a.MPdate);
            if (query.Count() > 0)
            {
                history = query.First();
            }
            return history;
        }
        /// <summary>
        /// 根据日期得到该交易日在分析系统中的插入日期
        /// </summary>
        /// <param name="pdate"></param>
        /// <returns></returns>
        public Models.Sys.TradeHistory GetSettlementModel(string pdate)
        {
            Models.Sys.TradeHistory revalue = new Models.Sys.TradeHistory();
            int year = Convert.ToInt32(pdate.Substring(0, 4));
            int month = Convert.ToInt32(pdate.Substring(4, 2));
            int day = Convert.ToInt32(pdate.Substring(6, 2));
            var query = from d in db.TradeHistorys where d.DPdate == day && d.MPdate == month && d.YPdate == year select d;
            if (query.Count() > 0)
            {
                revalue = query.First();
            }
            return revalue;
        }

        public Models.Sys.TradeHistory GetModel(int day, int month, int year)
        {
            var query = db.TradeHistorys.Where(a => a.DPdate == day).Where(a => a.MPdate == month).Where(a => a.YPdate == year);
            if (query.Count() > 0)
            {
                return query.First();
            }
            else
            {
                return new Models.Sys.TradeHistory();
            }
        }

        /// <summary>
        /// 根据日期得到该交易日在分析系统中的插入日期
        /// </summary>
        /// <param name="pdate"></param>
        /// <returns></returns>
        public DateTime GetPdateFromTradeHistoryId(int tradeHistoryId)
        {
            var query = db.TradeHistorys.Find(tradeHistoryId);
            int year = query.YPdate;
            int month = query.MPdate;
            int day = query.DPdate;
            string pdate = year + "-" + (month >= 10 ? month + "" : "0" + month) + "-" + (day >= 10 ? day + "" : "0" + day);
            return Convert.ToDateTime(pdate);
        }

        /// <summary>
        /// 根据日期得到该交易日在分析系统中的插入日期
        /// </summary>
        /// <param name="pdate"></param>
        /// <returns></returns>
        public string GetPdateFromSettlementId(int tradeHistoryId)
        {
            var query = db.TradeHistorys.Find(tradeHistoryId);
            int year = query.YPdate;
            int month = query.MPdate;
            int day = query.DPdate;
            string pdate = year + "" + (month >= 10 ? month + "" : "0" + month) + (day >= 10 ? day + "" : "0" + day);
            return pdate;
        }

        /// <summary>
        /// 保存导入记录ok
        /// </summary>
        /// <param name="pdate"></param>
        /// <returns></returns>
        public int Insert(string pdate)
        {
            Models.Sys.TradeHistory th = new Models.Sys.TradeHistory();
            th.DPdate = Convert.ToInt32(pdate.Substring(6, 2));
            th.MPdate = Convert.ToInt32(pdate.Substring(4, 2));
            th.YPdate = Convert.ToInt32(pdate.Substring(0, 4));
            DateTime d = Convert.ToDateTime(th.YPdate + "-" + th.MPdate + "-" + th.DPdate);
            th.WPdate = WeekOfYear(d);
            db.TradeHistorys.Add(th);
            db.SaveChanges();
            return th.Id;
        }
        /// <summary>
        /// 查询当日为当前年的第几个周
        /// </summary>
        /// <param name="day">查询日期</param>
        /// <param name="WeekStart">1表示 周一至周日 为一周,2表示 周日至周六 为一周</param>
        /// <returns></returns>
        public int WeekOfYear(DateTime pdate)
        {
            //先取当天的年份 
            string year = pdate.Year.ToString();
            //转换今年第一天 
            DateTime firstOfYear = DateTime.Parse("01   01   " + year);
            //计算当年第一天是星期几 
            int firstOfWeek = Convert.ToInt32(firstOfYear.DayOfWeek);
            //今天是今年的天数   +   当年第一天是星期几,   然后除   7   就可以了. 
            return (pdate.DayOfYear + firstOfWeek + 6) / 7;
        }
        /// <summary>
        /// 删除当日导入记录
        /// </summary>
        /// <param name="pdate"></param>
        public void Delete(string pdate)
        {
            Models.Sys.TradeHistory th = new Models.Sys.TradeHistory();
            th.DPdate = Convert.ToInt32(pdate.Substring(6, 2));
            th.MPdate = Convert.ToInt32(pdate.Substring(4, 2));
            th.YPdate = Convert.ToInt32(pdate.Substring(0, 4));
            var query = from t in db.TradeHistorys where t.DPdate == th.DPdate && t.MPdate == th.MPdate && t.YPdate == th.YPdate select t;
            if (query.Count() > 0)
            {
                th = query.First();
                db.TradeHistorys.Remove(th);
                db.SaveChanges();
            }
        }
        /// <summary>
        /// 得到一个对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Models.Sys.TradeHistory GetModel(int id)
        {
            Models.Sys.TradeHistory th = new Models.Sys.TradeHistory();
            var query = db.TradeHistorys.Where(a => a.Id == id);
            if (query.Count() > 0)
            {
                th = query.First();
            }
            return th;
        }

        /// <summary>
        /// 得到当前交易日的前五天的交易日期
        /// </summary>
        /// <returns></returns>
        public List<Models.Sys.TradeHistory> GetFrontFiveDayTradeHistoryIds(int tradeHistoryId)
        {
            List<Models.Sys.TradeHistory> ids = new List<Models.Sys.TradeHistory>();
            var query = from t in db.TradeHistorys where t.Id < tradeHistoryId orderby t.Id descending select t;
            ids = query.Take(5).ToList();
            return ids;
        }
        /// <summary>
        /// 获取数据到出日的时间ok
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        /// <returns></returns>
        public DateTime GetDateTimeFromTradeHistoryId(int tradeHistoryId)
        {
            Models.Sys.TradeHistory h = this.GetModel(tradeHistoryId);
            return Convert.ToDateTime(h.YPdate + "-" + h.MPdate + "-" + h.DPdate);
        }
        /// <summary>
        /// 得到某个周的所有的交易日
        /// </summary>
        /// <param name="week"></param>
        /// <param name="month"></param>
        /// <param name="year"></param>
        /// <returns></returns>
        public List<Models.Sys.TradeHistory> GetWeekTradeingDay(int week, int year)
        {
            var query = (db.TradeHistorys.Where(a => a.YPdate == year).Where(a => a.WPdate == week)).ToList();
            if (query.Count() > 0)
            {
                return query;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 得到周一共交易了多少天
        /// </summary>
        /// <param name="week"></param>
        /// <param name="year"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public int GetWeekTradeCount(int week, int year)
        {
            return GetWeekTradeingDay(week, year).Count();
        }

        /// <summary>
        /// 判断时间是否在时间段
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        public bool TimeInState(int state, int pdate)
        {
            bool inState = false;
            switch (state)
            {
                case 1:
                    if (pdate >= 90000 && pdate < 93000)
                    {
                        inState = true;
                    }
                    break;
                case 2:
                    if (pdate >= 93000 && pdate < 100000)
                    {
                        inState = true;
                    }
                    break;
                case 3:
                    if (pdate >= 100000 && pdate < 103000)
                    {
                        inState = true;
                    }
                    break;
                case 4:
                    if (pdate >= 103000 && pdate < 110000)
                    {
                        inState = true;
                    }
                    break;
                case 5:
                    if (pdate >= 130000 && pdate < 133000)
                    {
                        inState = true;
                    }
                    break;
                case 6:
                    if (pdate >= 133000 && pdate < 140000)
                    {
                        inState = true;
                    }
                    break;
                case 7:
                    if (pdate >= 140000 && pdate < 143000)
                    {
                        inState = true;
                    }
                    break;
                case 8:
                    if (pdate >= 143000 && pdate <= 150000)
                    {
                        inState = true;
                    }
                    break;
                default:
                    inState = false;
                    break;
            }


            return inState;
        }
        /// <summary>
        /// 得到最后一个交易日的结算编号
        /// </summary>
        /// <returns></returns>
        public int GetLastTradeHistoryId()
        {
            int id = 0;
            var query = db.TradeHistorys.OrderByDescending(a => a.Id);
            id = query.First().Id;
            return id;
        }

        /// <summary>
        /// 得到最后一个交易日的无分割线日期字符串
        /// </summary>
        /// <returns></returns>
        public string GetLastTradeHistoryDateString()
        {
            string date = "";
            var query = db.TradeHistorys.OrderByDescending(a => a.Id);
            if (query.Count() > 0)
            {
                Models.Sys.TradeHistory th = query.First();
                if (th != null)
                {
                    date = th.YPdate + "" + (th.MPdate < 10 ? ("0" + th.MPdate) : th.MPdate + "") + (th.DPdate < 10 ? ("0" + th.DPdate) : th.DPdate + "");
                }
            }
            return date;
        }

        /// <summary>
        /// 得到某个月的所有的交易日
        /// </summary>
        /// <param name="week"></param>
        /// <param name="month"></param>
        /// <param name="year"></param>
        /// <returns></returns>
        public List<Models.Sys.TradeHistory> GetMonthTradeingDay(int month, int year)
        {
            var query = (db.TradeHistorys.Where(a => a.YPdate == year).Where(a => a.MPdate == month)).ToList();
            if (query.Count() > 0)
            {
                return query;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 得到某年的所有的交易日
        /// </summary>
        /// <param name="week"></param>
        /// <param name="month"></param>
        /// <param name="year"></param>
        /// <returns></returns>
        public List<Models.Sys.TradeHistory> GetYearTradeingDay(int month, int year)
        {
            var query = (db.TradeHistorys.Where(a => a.YPdate == year).Where(a => a.MPdate == month)).ToList();
            if (query.Count() > 0)
            {
                return query;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到查询时间的前一个月的第一天
        /// </summary>
        /// <param name="month"></param>
        /// <param name="year"></param>
        /// <returns></returns>
        public DateTime GetLastMonth(int month, int year)
        {
            DateTime pdate = DateTime.Now;
            if (month == 1)
            {
                year = year - 1;
                month = 12;
            }
            else
            {
                month = month - 1;
            }
            pdate = Convert.ToDateTime("" + year + "-" + month + "-1");
            return pdate;
        }
        /// <summary>
        /// 查找周最后一个交易日
        /// </summary>
        /// <param name="week"></param>
        /// <param name="year"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public Models.Sys.TradeHistory GetWeekLastTradingDay(int week, int year)
        {
            Models.Sys.TradeHistory th = new Models.Sys.TradeHistory();
            var query = db.TradeHistorys.Where(a => a.WPdate == week).Where(a => a.YPdate == year).OrderByDescending(a => a.DPdate);
            if (query.Count() > 0)
            {
                th = query.First();
            }
            return th;
        }
        /// <summary>
        /// 查找周第一个交易日
        /// </summary>
        /// <param name="week"></param>
        /// <param name="year"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public Models.Sys.TradeHistory GetWeekFirstTradingDay(int week, int year)
        {
            Models.Sys.TradeHistory th = new Models.Sys.TradeHistory();
            var query = db.TradeHistorys.Where(a => a.WPdate == week).Where(a => a.YPdate == year).OrderBy(a => a.DPdate);
            if (query.Count() > 0)
            {
                th = query.First();
            }
            return th;
        }



        public List<Models.Sys.TradeHistory> GetList()
        {
            return db.TradeHistorys.ToList();
        }
    }
}